<?php

namespace Gds\VenteBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Gds\VenteBundle\Entity\TicketCaisse
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Gds\VenteBundle\Entity\TicketCaisseRepository")
 */
class TicketCaisse
{
    /**
     * @ORM\ManyToOne(targetEntity="Gds\VenteBundle\Entity\Client", inversedBy="tickets")
     * @ORM\JoinColumn(nullable=false)
     */
    private $client;
    /**
     * @ORM\OneToMany(targetEntity="Gds\VenteBundle\Entity\LigneTicketCaisse", mappedBy="ticket",cascade={"persist"})
     */
    private $lignes; // Ici commentaires prend un "s", car un article a plusieurs commentaires !
    /**
     * @ORM\OneToMany(targetEntity="Gds\VenteBundle\Entity\LigneReglementClient", mappedBy="ticket")
     */
    private $lignes_reglements; // Ici commentaires prend un "s", car un article a plusieurs commentaires !
    /**
     * @ORM\ManyToOne(targetEntity="Gds\VenteBundle\Entity\SessionCaisse", inversedBy="tickets")
     * @ORM\JoinColumn(nullable=false)
     */
    private $session;
    /**
     * @ORM\OneToOne(targetEntity="Gds\VenteBundle\Entity\Acompte",cascade={"persist"})
     */
    private $acompte;
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var float $reste
     *
     * @ORM\Column(name="reste", type="decimal", scale=3)
     */
    private $reste;

    /**
     * @var float $montant_ttc
     *
     * @ORM\Column(name="montant_blanc_ttc", type="decimal", scale=3)
     */
    private $montant_blanc_ttc;

    /**
     * @var float $montant_noir
     *
     * @ORM\Column(name="montant_noir_ttc", type="decimal", scale=3)
     */
    private $montant_noir_ttc;

    /**
     * @var \DateTime $date
     *
     * @ORM\Column(name="date", type="datetime")
     */
    private $date;

 /**
     * @var string $reference
     *
     * @ORM\Column(name="reference", type="string", length=255)
     */
    private $reference;
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    

    /**
     * Set date
     *
     * @param \DateTime $date
     * @return TicketCaisse
     */
    public function setDate($date)
    {
        $this->date = $date;
    
        return $this;
    }

    /**
     * Get date
     *
     * @return \DateTime 
     */
    public function getDate()
    {
        return $this->date;
    }

    /**
     * Set session
     *
     * @param Gds\VenteBundle\Entity\SessionCaisse $session
     * @return TicketCaisse
     */
    public function setSession(\Gds\VenteBundle\Entity\SessionCaisse $session)
    {
        $this->session = $session;
    
        return $this;
    }

    /**
     * Get session
     *
     * @return Gds\VenteBundle\Entity\SessionCaisse 
     */
    public function getSession()
    {
        return $this->session;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->lignes = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    /**
     * Add lignes
     *
     * @param Gds\VenteBundle\Entity\LigneTicketCaisse $lignes
     * @return TicketCaisse
     */
    public function addLigne(\Gds\VenteBundle\Entity\LigneTicketCaisse $lignes)
    {
        $this->lignes[] = $lignes;
    $lignes->setTicket($this);
        return $this;
    }

    /**
     * Remove lignes
     *
     * @param Gds\VenteBundle\Entity\LigneTicketCaisse $lignes
     */
    public function removeLigne(\Gds\VenteBundle\Entity\LigneTicketCaisse $lignes)
    {
        $this->lignes->removeElement($lignes);
    }

    /**
     * Get lignes
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getLignes()
    {
        return $this->lignes;
    }

    /**
     * Add lignes_reglements
     *
     * @param Gds\VenteBundle\Entity\LigneReglementClient $lignesReglements
     * @return TicketCaisse
     */
    public function addLignesReglement(\Gds\VenteBundle\Entity\LigneReglementClient $lignesReglements)
    {
        $this->lignes_reglements[] = $lignesReglements;
    
        return $this;
    }

    /**
     * Remove lignes_reglements
     *
     * @param Gds\VenteBundle\Entity\LigneReglementClient $lignesReglements
     */
    public function removeLignesReglement(\Gds\VenteBundle\Entity\LigneReglementClient $lignesReglements)
    {
        $this->lignes_reglements->removeElement($lignesReglements);
    }

    /**
     * Get lignes_reglements
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getLignesReglements()
    {
        return $this->lignes_reglements;
    }

    /**
     * Set client
     *
     * @param Gds\VenteBundle\Entity\Client $client
     * @return TicketCaisse
     */
    public function setClient(\Gds\VenteBundle\Entity\Client $client)
    {
        $this->client = $client;
    
        return $this;
    }

    /**
     * Get client
     *
     * @return Gds\VenteBundle\Entity\Client 
     */
    public function getClient()
    {
        return $this->client;
    }

    /**
     * Set reste
     *
     * @param float $reste
     * @return TicketCaisse
     */
    public function setReste($reste)
    {
        $this->reste = $reste;

        return $this;
    }

    /**
     * Get reste
     *
     * @return float 
     */
    public function getReste()
    {
        return $this->reste;
    }

    /**
     * Set montant_blanc_ttc
     *
     * @param float $montantBlancTtc
     * @return TicketCaisse
     */
    public function setMontantBlancTtc($montantBlancTtc)
    {
        $this->montant_blanc_ttc = $montantBlancTtc;

        return $this;
    }

    /**
     * Get montant_blanc_ttc
     *
     * @return float 
     */
    public function getMontantBlancTtc()
    {
        return $this->montant_blanc_ttc;
    }

    /**
     * Set montant_noir_ttc
     *
     * @param float $montantNoirTtc
     * @return TicketCaisse
     */
    public function setMontantNoirTtc($montantNoirTtc)
    {
        $this->montant_noir_ttc = $montantNoirTtc;

        return $this;
    }

    /**
     * Get montant_noir_ttc
     *
     * @return float 
     */
    public function getMontantNoirTtc()
    {
        return $this->montant_noir_ttc;
    }

    /**
     * Set reference
     *
     * @param string $reference
     * @return TicketCaisse
     */
    public function setReference($reference)
    {
        $this->reference = $reference;

        return $this;
    }

    /**
     * Get reference
     *
     * @return string 
     */
    public function getReference()
    {
        return $this->reference;
    }
    public function getMontant(){
        $montant= $this->montant_blanc_ttc+$this->montant_noir_ttc;
        if($this->acompte)
            $montant+=$this->getAcompte ()->getMontant ();
        return $montant-$this->getMontantAccompte();
    }
    

    /**
     * Set acompte
     *
     * @param \Gds\VenteBundle\Entity\Acompte $acompte
     * @return TicketCaisse
     */
    public function setAcompte(\Gds\VenteBundle\Entity\Acompte $acompte = null)
    {
        $this->acompte = $acompte;

        return $this;
    }

    /**
     * Get acompte
     *
     * @return \Gds\VenteBundle\Entity\Acompte 
     */
    public function getAcompte()
    {
        return $this->acompte;
    }
    public function getMontantEspese()
    {
        $montant= 0;
        $lignes= $this->getLignesReglements();
        foreach ($lignes as $ligne){
            if($ligne->getType() == 'Espece'){
                $montant+=$ligne->getMontant();
            }
        }
        return $montant;
    }
    public function getMontantCheque()
    {
        $montant= 0;
        $lignes= $this->getLignesReglements();
        foreach ($lignes as $ligne){
            if($ligne->getType() == 'Cheque'){
                $montant+=$ligne->getMontant();
            }
        }
        return $montant;
    }
    public function getMontantEffet()
    {
        $montant= 0;
        $lignes= $this->getLignesReglements();
        foreach ($lignes as $ligne){
            if($ligne->getType() == 'Effet'){
                $montant+=$ligne->getMontant();
            }
        }
        return $montant;
    }
    public function getMontantAccompte()
    {
        $montant= 0;
        $lignes= $this->getLignesReglements();
        foreach ($lignes as $ligne){
            if($ligne->getType() == 'Acompte'){
                $montant+=$ligne->getMontant();
            }
        }
        return $montant;
    }
    public function getMontantRetour()
    {
        $montant= 0;
        $lignes= $this->getLignes();
        foreach ($lignes as $ligne){
            $montant+=$ligne->getQuantiteRetour()*$ligne->getPrixUnitaireHt()*(1+($ligne->getTauxTva()/100))*(1-($ligne->getTauxRemise()/100));
        }
        return $montant;
    }
    public function getMontantVente()
    {
        $montant= 0;
        $lignes= $this->getLignes();
        foreach ($lignes as $ligne){
            $montant+=$ligne->getQuantite()*$ligne->getPrixUnitaireHt()*(1+($ligne->getTauxTva()/100))*(1-($ligne->getTauxRemise()/100));
        }
        return $montant;
    }
    
}
